<%@ Control Language="C#" AutoEventWireup="true" Inherits="Portal.Data.Webparts.WebpartControl" %>
<%@ Import Namespace="Portal.Core" %>
<%@ Import Namespace="Portal.Core.Helper" %>
<%@ Import Namespace="Portal.Data.Entity" %>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["catname"] == null)
            return;

        //Build News
        string sRss = BuildNews();

        Response.ClearContent();
        Response.ContentType = "text/xml";

        //sRSS = Process.Replace("&", "&amp;", sRSS);

        Response.Write(sRss);
        Response.End();
    }

    private string BuildNews()
    {
        var feed = new Rss();

        //Add channel
        var channel = new Rss.RssChannel
        {
            Title = Common.GetString("Default", "PortalTitle"),
            Link = string.Format("{0}rss/home/", CoreContext.basepath),
            Description = Common.GetString("Default", "META_Description")
        };
        feed.AddRssChannel(channel);

        //add items
        var books = new List<Book>();
        books.AddRange(Book.FindLatestUpdate(0));
        books.AddRange(Book.FindLatestUpdate(1));

        foreach (Book book in books)
        {
            var item = new Rss.RssItem { Title = book.FullName, Link = book.LastChapUrl };

            string sImg = "";
            if (!string.IsNullOrEmpty(book.Image))
                sImg = string.Format(
                            "<a href=\"{0}\"><img src=\"{1}\" align='left' border='0' hspace='5' alt=\"{2}\" /></a>",
                            book.Url, Book.RenderImage(book.Image, 100), book.LastChapTitle + " | " + book.FullName);
            item.Description = string.Format("{0}{1}", sImg, book.DescriptionSort());

            item.PubDate = book.LastChap.CreatedDate.ToString("r");
            feed.AddRssItem(item);
        }

        return feed.RssDocument;
    }
</script>
